package cz.cernilovsky.android.profileapp.activities;

import java.io.IOException;


import org.json.JSONException;

import cz.cernilovsky.android.profileapp.R;
import cz.cernilovsky.android.profileapp.model.Location;
import cz.cernilovsky.android.profileapp.services.NotificationService;
import cz.cernilovsky.android.profileapp.services.ToastService;
import cz.cernilovsky.android.profileapp.storage.LocationsStorage;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class LocationsFragmentListActivity extends ListFragment {

	public static final String PROFILES = "profiles";

/*	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}*/

	private void loadLocations(Location[] locations) {
		if(locations == null) {
			locations = new Location[0];
		}
		setListAdapter(new Adapter(locations));
		System.out.println("Loaded profiles in fragment");

	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent i = new Intent(getActivity(), AddLocationActivity.class);
		i.putExtra(AddLocationActivity.LOCATIONS, ((LocationView) v).getLocation());
		startActivity(i);
	}

	void loadLocations() {
		try {
			Location[] locations = LocationsStorage.loadLocations(getActivity());
			loadLocations(locations);
		} catch (IOException e) {
			ToastService.toastSettingsIOError(getActivity());
			e.printStackTrace();
		} catch (JSONException e) {
			ToastService.toastSettingsJSONError(getActivity());
			e.printStackTrace();
		}
	}
	
	class Adapter extends BaseAdapter {
		private Location[] locations;
		public Adapter(Location[] locations) {
			this.locations = locations;
		}

		@Override
		public int getCount() {
			return locations.length;
		}

		@Override
		public Object getItem(int i) {
			return locations[i];
		}

		@Override
		public long getItemId(int i) {
			return i;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Location location = (Location) getItem(position);
			View view = null;
			if(convertView == null) {
				view = new LocationView(getActivity(), location);
			} else {
				((LocationView) convertView).setLocation(location);
				view = convertView;
			}
			return view;
		}
		
	}
	
	class LocationView extends LinearLayout {
		private TextView locationName;
		private Button delete;
		private Location location;

		public LocationView(Context context, Location location) {
			super(context);
			this.location = location;
			locationName = new TextView(context);
			locationName.setTextSize(20);
			LayoutParams params = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
			locationName.setLayoutParams(params);
			delete = new Button(context);
			delete.setText(R.string.delete);
			//setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			setOrientation(LinearLayout.HORIZONTAL);
			setLocation(location);
			addView(locationName);
			addView(delete);
			setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
		}

		public void setLocation(final Location location) {
			this.location = location;
			locationName.setText(location.getName());
			delete.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					try {
						LocationsStorage.deletelocation(location, LocationsFragmentListActivity.this.getActivity());
						loadLocations();
					} catch (IOException e) {
						ToastService.toastSettingsIOError(getActivity());
						e.printStackTrace();
					} catch (JSONException e) {
						ToastService.toastSettingsJSONError(getActivity());
						e.printStackTrace();
					}
				}
			});
		}
		
		public Location getLocation() {
			return location;
		}
		
	}

	@Override
	public void onStart() {
		super.onStart();
		loadLocations();
	}

}
